<?php
include '../includes/cPlayer.php';
include '../includes/cDatabase.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Change Active Database");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

if(@$_GET['change'] == "yes")
{
	$cDatabase = new database;
	$connect = $cDatabase->connectToDatabase();
	$db = mysqli_select_db($connect, ffl_config);

	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'configList', $selectArray, NULL, NULL, NULL, NULL);
	
	$finfo = mysqli_fetch_fields($result);

	if($result == 0)
	{
		echo "<b>Error ".mysqli_errno($connect).": ".mysqli_error($connect)."</b>";
	}
	else if (@mysqli_num_rows($result) == 0)
	{
    	// There are no entries at current, put this data in for the first time
		//$query = "INSERT INTO configlist (loginName, password, activeDatabase) 
		//	VALUES ('".$this->user."', '".$this->password."', 'ffl')";
	}
	else
	{
		// Update this entry
		$query = "UPDATE configlist SET activeDatabase = '".$database."' WHERE loginName='root'";
	}
	
	$result = mysqli_query($connect, $query);
	
	if ($result == 1)
	{
		echo"<BR>Active database has been changed to ".$database;
	}
	else
	{
		echo"<BR>ERROR: Active database has not been changed.";
	}
}
else
{
	$PHP_SELF = $_SERVER['PHP_SELF'];
	$error = 0;
	
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);

	echo "<BR>Current active database is ".$activeDatabase;
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);
	$query = "SHOW DATABASES";
	$error = mysqli_query($connect, $query);

	$finfo = mysqli_fetch_fields($error);
	if($error == 0)
	{
		echo "<b>Error ".mysqli_errno($connect).": ".mysqli_error($connect)."</b>";
	}
	else if (@mysqli_num_rows($error) == 0)
	{
    	echo "<b>Query completed. No results returned.</b><br>";
	}
	else
	{
		echo "<FORM ACTION=\"$PHP_SELF?change=yes\" METHOD=\"POST\">";
		echo "<table width='400' border='0'>";
		echo "<td><select name='database'>";
		for ($i = 0; $i < mysqli_num_rows($error); $i++)
		{
			$row = mysqli_fetch_row($error);
			for($j = 0; $j < mysqli_num_fields($error); $j++)
			{
				echo "<option>".$row[$j]."</option>";
			}
		}
		echo "</select></td>";
		echo "<td><input name='databaseButton' type='submit' id='databaseSubmit' value='Update'></td>";
		echo "</table></form>";
	}
}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>